package com.wbdgj.ui.home;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.adapter.MessageAdapter;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.base.BaseFragment;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultMessageObjectModel;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.near.MessageBoardDetailActivity;
import com.wbdgj.utils.EasyAbsListView;
import com.wbdgj.utils.ListViewForScrollView;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/5/2.
 */

public class WbDetail03Fragment extends BaseFragment{

    private Context context;
    private String id;

    @BindView(R.id.demo_listview)
    ListViewForScrollView demo_listview;

    @BindView(R.id.moreLay)
    LinearLayout moreLay;

    private int page = 1;
    private int totalPage = 10;

    private MessageAdapter messageAdapter;
    @Override
    protected int getLayout() {
        return R.layout.ui_wb_detail_03;
    }

    @Override
    protected void onInit() {
        context = getActivity();
        id = getActivity().getIntent().getStringExtra("id");
        demo_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (!messageAdapter.hasNoData){
                    BaseApplication.getInstance().setLinkedTreeMap(messageAdapter.getItem(i));
                    startActivity(new Intent(context, MessageBoardDetailActivity.class).putExtra("branch_id", id));
                }
            }
        });
    }

    @OnClick({R.id.moreLay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.moreLay:
                startActivity(new Intent(context, MessageListActivity.class).putExtra("id", id));
                break;

        }
    }

    @Override
    public void fetchData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        listAll();
    }

    private void listAll() {
        Call<ResultMessageObjectModel> call = HttpAdapter.getSerives().msgList("", ""+totalPage, ""+page,id, "", "", "3");
        call.enqueue(new OnResponseListener<ResultMessageObjectModel>(context) {
            @Override
            protected void onSuccess(ResultMessageObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getCode() == 200){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    ArrayList<LinkedTreeMap> arrayList = (ArrayList<LinkedTreeMap>) userModelBaseResultModel.getData();
                    messageAdapter = new MessageAdapter(context, arrayList);
                    demo_listview.setAdapter(messageAdapter);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getMsg());
                }
            }
        });
    }
}
